import json
import requests



# 获取access_token
host = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=ATbNb012Gmq1FN4pAL5Dr11U&client_secret=8phRQiLl0bzfpVGswFmQtK4V87o49v83"
response = requests.get(host)
mytoken = response.json()['access_token']

headers = {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
}



# 根据感情倾向进行可视化呈现
def vis_process(percent):
    repeat_times = int(percent * 10)
    bar = '感情正向->' + '😊' * repeat_times + '😭' * (10 - repeat_times) + '<-感情负向'
    print(bar)


def txt_test(mystr, mytoken):
    content_txt = json.dumps({
        'text': mystr
    })

    url1 = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify'
    myurl = url1 + '?charset=UTF-8&access_token=' + mytoken
    results = requests.post(url=myurl, headers=headers, data=content_txt).json()

    input_txt = results['text']
    positive_prob = results['items'][0]['positive_prob']
    print(input_txt)
    vis_process(positive_prob)
    print('-' * 20 + '分割线' + '-' * 20)

txt_test("Mr.IDon'tCare其实也有Care的时候。", mytoken)